﻿using PixivVideo.Currency.IoUtils;
using PixivVideo.Utils.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace PixivVideo.Currency
{
    internal class PrintLog
    {
        /// <summary>
        /// string format  支持三个参数拼接。
        /// </summary>
        /// <param name="format"></param>
        /// <param name="arg0"></param>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        public static void Log(string format, object arg0, object arg1, object arg2)
        {
            String text = string.Format(format, arg0, arg1, arg2);
            Console.WriteLine(text);
            WriteLog(text);
        }

        /// <summary>
        /// 定位打印，需要指定显示的位置。
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void WriteAt(string Str, int x = 0, int y = 0)
        {
            try
            {
                Console.SetCursorPosition(x, y);
                Console.Write(Str);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.Clear();
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// 打印百分比Log
        /// </summary>
        /// <param name="AllCount"></param>
        /// <param name="ComplateCount"></param>
        /// <param name="LogMessage"></param>
        public void PercentLog(int AllCount, int ComplateCount, string LogMessage = "总数{0}，已处理{1},占比{2}%")
        {
            Log(string.Format(LogMessage, AllCount, ComplateCount, ((float)ComplateCount / AllCount) * 100));
        }

        /// <summary>
        /// 帮助你写入一个
        /// </summary>
        /// <param name="Content"></param>
        public static void RandomLog(string Content, string Label = "随机日志")
        {
            FileHelper.AppendUTF8Text(StaticValue.TempPath + Label + DateTime.Now.ToFileTime() + ".txt", Content);
        }

        public static void ReflushLog(string LogPath, String Text, List<string> LogList)
        {
            FileHelper.AppendUTF8Text(LogPath, Text);
            LogList.Add(Text);
        }

        /// <summary>
        /// 缩短日志
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Count"></param>
        public static void LogNarrow(string FilePath, int Count = 3000)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in new ListHelper().ListNarrow(FilePath, Count))
            {
                if (Line != "")
                    TempList.Add(Line);
            }
            FileHelper.WriteUTF8List(FilePath, TempList);
        }

        /// <summary>
        /// string format  支持两个个参数拼接。
        /// </summary>
        /// <param name="format"></param>
        /// <param name="arg0"></param>
        /// <param name="arg1"></param>

        public static void Log(string format, object arg0, object arg1)
        {
            String text = string.Format(format, arg0, arg1);
            Console.WriteLine(text);
            WriteLog(text);
        }

        /// <summary>
        ///  string format  支持一个参数拼接。
        /// </summary>
        /// <param name="format"></param>
        /// <param name="arg0"></param>
        public static void Log(string format, object arg0)
        {
            String text = string.Format(format, arg0);
            Console.WriteLine(text);
            WriteLog(text);
        }

        public static void Log(String Log)
        {
            Console.WriteLine(Log);
            WriteLog(Log);
        }

        private static void WriteLog(string Log)
        {
            string ErrorLogPath = StaticValue.TempPath + "errorlog";
            String GetFloder = Path.GetDirectoryName(ErrorLogPath);
            if (!Directory.Exists(GetFloder))
                Directory.CreateDirectory(GetFloder);//由于printLog要复制多个项目，故专门实现一次
            try { File.AppendAllText(ErrorLogPath, "\r\n" + Log, Encoding.UTF8); }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public static void Log(System.Exception Log)
        {
            String text = Log.ToString();
            Console.WriteLine(text);
            WriteLog(text);
        }

        public static void Log(List<String> Log)
        {
            foreach (String line in Log)
            {
                Console.WriteLine(line);
                WriteLog(line);
            }
        }
    }
}